<?php
class ControllerMyplanRegister extends Controller{
	public function index(){

		$this->data["login"]=$this->url->link("myplan/login");
		$this->data["action"]=$this->url->link("myplan/register/register");

	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplan/register.tpl')) 		            {
				$this->template = $this->config->get('config_template') . '/template/myplan/register.tpl';
			} else {
				$this->template = 'default/template/myplan/register.tpl';
			}
											
			$this->response->setOutput($this->render());
	}

	public function register(){
	    if($this->request->server["REQUEST_METHOD"]=="POST"){

		    $this->load->model("myplan/common");

		if($this->validate()){


			$this->model_myplan_common->registerUser($this->request->post);

			$this->data["success"]="注册成功.";
		}

		$this->index();
		
	    }    
	}

	private function validate(){

		    if($this->request->post["password"]!=$this->request->post["repassword"]){
			    $this->data["warning"]="重复密码错误.";
			    return false;
		    } 

		    if($this->request->post["password"]==""||$this->request->post["account"]==""||$this->request->post["nickname"]==""){
			    $this->data["warning"]="不能为空.";
			    return false;
		    }
			
		    if($this->model_myplan_common->login($this->request->post)->num_rows==1){
			    $this->data["warning"]="账号已存在.";
			    return false;
		    }

		    return true;

	}
}
?>
